Service.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. "use client";
  2. import { ServiceTypes } from "@/api/customservice";
  3. import { lredPacketApi, redPacketApi } from "@/api/promo";
  4. import RedPacketModal, { RedPacketModalProps } from "@/components/Box/RedPacketModal";
  5. import { Link } from "@/i18n";
  6. import { useGlobalNoticeStore } from "@/stores/useGlobalNoticeStore";
  7. import { useSocialStore } from "@/stores/useSocials";
  8. import { getToken } from "@/utils/Cookies";
  9. import { useRequest } from "ahooks";
  10. import { Badge } from "antd-mobile";
  11. import { useTranslations } from "next-intl";
  12. import { FC, useEffect, useRef, useState } from "react";
  13. interface Props {
  14. services: ServiceTypes[];
  15. socials: ServiceTypes[];
  16. }
  17. const ServiceWidget: FC<Props> = (props) => {
  18. const token = getToken();
  19. const [packets, setPackets] = useState<any[]>([]);
  20. const { services, socials } = props;
  21. const defaultService = services.find((item) => item.is_suspend === 1);
  22. const newServices = services.filter((item) => item.is_suspend !== 1);
  23. const setSocials = useSocialStore((state) => state.setSocials);
  24. const RedPacketModalRef = useRef<RedPacketModalProps>(null);
  25. useEffect(() => {
  26. // 数据存储,侧边栏使用
  27. setSocials(socials);
  28. }, []);
  29. const t = useTranslations("HomePage");
  30. const { unread } = useGlobalNoticeStore((state) => ({
  31. unread: state.unread,
  32. }));
  33. const getRedPacketInfo = async () => {
  34. try {
  35. let redPacketInfo: any;
  36. let actList: any = [];
  37. if (token) {
  38. redPacketInfo = await lredPacketApi();
  39. actList = redPacketInfo.data?.red_packets || [];
  40. } else {
  41. redPacketInfo = await redPacketApi();
  42. actList = redPacketInfo.data || [];
  43. }
  44. // 是否有已开始但是没领过的红包
  45. let isHasStartAct = actList.filter((aItem: any) => {
  46. return aItem.is_start && !aItem.is_receive;
  47. });
  48. console.log(`🚀🚀🚀🚀🚀-> in Service.tsx on 53`, isHasStartAct);
  49. setPackets(isHasStartAct);
  50. } catch (error) {
  51. console.log("redPacketInfo===>error:", error);
  52. }
  53. };
  54. // 红包雨轮询
  55. useRequest(getRedPacketInfo, {
  56. pollingInterval: 180000,
  57. pollingErrorRetryCount: 1,
  58. pollingWhenHidden: false,
  59. onSuccess: (data) => {
  60. console.log("data", data);
  61. },
  62. });
  63. return (
  64. <>
  65. {/* 红包雨icon */}
  66. {packets.map((item, index) => {
  67. return (
  68. <div
  69. key={index}
  70. className={`absolute right-[0.12rem] z-50 flex h-[0.54rem] w-[0.54rem] cursor-pointer items-center justify-center`}
  71. style={{ bottom: `${2.04 + index * 0.5}rem` }}
  72. >
  73. <img
  74. className={"h-[0.3889rem] w-[0.3889rem]"}
  75. src="/hby/red-icon.png"
  76. onClick={() => {
  77. RedPacketModalRef.current?.onOpen(index);
  78. }}
  79. />
  80. </div>
  81. );
  82. })}
  83. {/* 红包弹窗 */}
  84. {/*{isShowRedPacket && (*/}
  85. {/* <PopupHby*/}
  86. {/* onClose={() => {*/}
  87. {/* setIsShowRedPacket(false);*/}
  88. {/* }}*/}
  89. {/* />*/}
  90. {/*)}*/}
  91. <RedPacketModal ref={RedPacketModalRef} />
  92. {defaultService && (
  93. <Link
  94. href={defaultService.url}
  95. className={
  96. "flex h-[0.54rem] w-[0.54rem] items-center justify-center rounded-[50%]" +
  97. " absolut absolute bottom-[0.84rem] right-[0.12rem] z-50 bg-gradient-to-b from-[#ff6a01] to-primary-color"
  98. }
  99. target={"_blank"}
  100. >
  101. <img
  102. className={"h-[0.3889rem] w-[0.3889rem]"}
  103. src={defaultService.icon_url}
  104. ></img>
  105. </Link>
  106. )}
  107. <Link
  108. href={"/notification"}
  109. className={
  110. "flex h-[0.54rem] w-[0.54rem] items-center" +
  111. " absolute bottom-[1.44rem] right-[0.12rem] z-50 justify-center rounded-[50%] bg-gradient-to-b from-[#0575e6] to-[#00f260]"
  112. }
  113. >
  114. <Badge content={!!unread ? unread : null} style={{ "--top": "12px" }}>
  115. <i className={"iconfont icon-duanxinguanli text-[0.3rem] text-[#fff]"}></i>
  116. </Badge>
  117. </Link>
  118. <div className={`grid grid-cols-${newServices.length >= 5 ? 5 : newServices.length}`}>
  119. {newServices.map((service, index) => {
  120. return (
  121. <Link
  122. key={index}
  123. href={service.url}
  124. target={"_blank"}
  125. className="bg-white m-[0.05rem] h-[0.3889rem] w-[0.3889rem] rounded"
  126. >
  127. <img
  128. className={"h-[0.3889rem] w-[0.3889rem]"}
  129. src={service.icon_url}
  130. ></img>
  131. </Link>
  132. );
  133. })}
  134. </div>
  135. <div className={"text-[#ff6a01]"}>{t("Service")}</div>
  136. {/*share*/}
  137. <div className={"my-[0.2rem] text-[0.08rem] text-[#adadad]"}>{t("Share")}</div>
  138. <div className={`grid grid-cols-${socials.length >= 5 ? 5 : socials.length}`}>
  139. {socials.map((service, index) => {
  140. return (
  141. <Link
  142. key={index}
  143. href={service.url}
  144. target={"_blank"}
  145. className="bg-white m-[0.05rem] h-[0.3889rem] w-[0.3889rem] rounded"
  146. >
  147. <img
  148. className={"h-[0.3889rem] w-[0.3889rem]"}
  149. src={service.icon_url}
  150. ></img>
  151. </Link>
  152. );
  153. })}
  154. </div>
  155. </>
  156. );
  157. };
  158. export default ServiceWidget;